package com.xy.mainp.base;

import android.util.Log;

/**
 * Created by John on 2016/11/21.
 */

public class LazyFragment extends BasePFragment {

    private boolean isShow = false;
    private boolean isVisiable = false;
    private boolean isFirst = true;
    @Override
    public final void onResume() {
        super.onResume();
        if (isFirst){  //判断是否是第一次进入fragment
            isFirst = false;
            setUserVisibleHint(true);
        }else if (isVisiable){
            show();
        }
    }



    @Override
    public final void onPause() {
        super.onPause();
        if (isVisiable){
            hide();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {  //使用setUserVisibleHint，每次都会调用
        super.onHiddenChanged(hidden);
        setUserVisibleHint(!hidden);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {  //用于判断fragment是否是可见的
        isFirst = false;
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()){
            in();
        }else {
            out();
        }

    }
    private void in() {
        if (!isVisiable){
            isVisiable = true;
            onIn();
            show();
        }
    }
    private void out() {
        if (isVisiable){
            isVisiable = false;
            onOut();
            hide();
        }
    }

    private void show() {
        if (!isShow){
            isShow = true;
            onShow();
        }
    }



    private void hide() {
        if (isShow){
            isShow = false;
            onHide();
        }
    }

    protected void onOut() {
        Log.i("lazyF", "onOut");
    }

    protected void onIn() {
        Log.i("lazyF", "onIn");
    }

    protected void onShow() {
        Log.i("lazyF", "onShow");
    }

    protected void onHide() {
        Log.i("lazyF", "onHide");
    }

}
